package com.group3.hospital.shiro;/*
 *@author 时崎
 * 2021/12/30
 */

import com.group3.hospital.shiro.ShiroUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.spring.web.config.DefaultShiroFilterChainDefinition;
import org.apache.shiro.spring.web.config.ShiroFilterChainDefinition;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShiroConfig {
    //注入Realm
    @Bean
    public ShiroRealm shiroRealm(){
        ShiroRealm shiroRealm = new ShiroRealm();
        //修改凭证校验匹配器
        HashedCredentialsMatcher credentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法为md5
        credentialsMatcher.setHashAlgorithmName(ShiroUtils.ALGORITH_NAME);
    //设置散列次数
    credentialsMatcher.setHashIterations(ShiroUtils.HASH_ITERATIONS);
    shiroRealm.setCredentialsMatcher(credentialsMatcher);
        return shiroRealm;
    }

   @Bean
    DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager manager = new DefaultWebSecurityManager();
         manager.setRealm(shiroRealm());
         return manager;
    }

    @Bean


    ShiroFilterChainDefinition shiroFilterChainDefinition() {
        DefaultShiroFilterChainDefinition definition = new
                        DefaultShiroFilterChainDefinition();
        //anon 资源为公共资源，放行
        definition.addPathDefinition("/loginUi", "anon");
        definition.addPathDefinition("/registUi", "anon");
        definition.addPathDefinition("/regist", "anon");
        definition.addPathDefinition("/login", "anon");
        //authc 资源需要认证和授权
        definition.addPathDefinition("/admin/**", "authc");
        return definition;
    }
}